﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PS.DataTypes;

namespace PS.Engine.GameComponents
{
    internal class Obstacle : PolygonComponent
    {
        #region Private Const Members

        private const int WaypointDistance = 20;

        #endregion Private Const Members

        #region Private Members

        private List<Vector2> waypoints = null;

        #endregion Private Members

        #region Constructors

        public Obstacle(Game game)
            : base(game)
        {
        }

        #endregion

        #region Properties

        public List<Vector2> Waypoints
        {
            get
            {
                if (waypoints == null)
                {
                    LoadWaypoints();
                }

                return waypoints;
            }
        }

        #endregion Properties

        #region Private Methods

        private void LoadWaypoints()
        {
            waypoints = new List<Vector2>();
            
            foreach (Line edge in Edges)
            {
                // get the edge's vector
                Vector2 edgeVector = edge.To - edge.From;

                // normalize the vector of the edge
                Vector2 edgeVectorNormalized = new Vector2(edgeVector.X, edgeVector.Y);
                edgeVectorNormalized.Normalize();

                // calculate both waypoints
                Vector2 waypoint1 = edge.From - edgeVectorNormalized * WaypointDistance;
                Vector2 waypoint2 = edge.To + edgeVectorNormalized * WaypointDistance;

                // add the waypoints
                waypoints.Add(waypoint1);
                waypoints.Add(waypoint2);
            }
        }

        #endregion Private Methods

        #region Lifecycle overrides

        /// <summary>
        /// Loads the specific content (e.g. texture) for the Pedestrian class.
        /// </summary>
        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>("Obstacle");
            base.LoadContent();
        }

        #endregion
    }
}
